var app = new Vue({
  el: '#app',
  data () {
    return {
      brandName: "", // 品牌关键字
      fullName: "", // 商品名关键字
      valueName: "", // 规格关键字
      resultList: null, // 搜索结果列表数据
      brandData: [], // 品牌列表数据
      dialogShow: false, // 弹出层是否显示
      otherMarket: false, // 其它超市弹出层是否显示
      cartShow: false, // 加入购物车弹出层是否显示
      tipshow: false, // 信息提示框弹出层是否显示
      marketlist: null, // 其它超市列表数据
      name: "", // 其它超市商品名
      spreadRate: "", // 价差率
      num: 1, // 加入购物车弹出层商品数量
      curIndex: 0, // 当前下标
      errMsg: "", // Toast提示框提示文字
      tips: "", // 信息提示框弹出层提示文字
      Top: "", // 品牌输入框top
      Height: "", // 品牌输入框高度
      Width: "", // 品牌输入框宽度
      Left: "", // 品牌输入框left
      currentPage: -1, // 当前页数
      totalPage: 2, // 总页数
      pageSize: 8, // 每页显示条数
      loading: false,
      isTap: false,
      show: true,
      readonly: false
    }
  },
  watch: {
    brandName (val) {
      if (val!=="" && this.isTap == false) {
        this.getBrandName(val)
      }
    }/* ,
    errMsg: function (newerrMsg, olderrMsg) {
      setTimeout(function() { 
        app.errMsg = ''    
      }, 1000);
    } */
  },  
  computed: {
    Style () {      
      return this.brandName === "" ? {
        top: this.Top + this.Height,
        width: this.Width,
        left: this.Left
      } : {
        top: this.Top + this.Height,
        width: this.Width,
        left: this.Left
      }
    },
    brandlist: function () {
      return this.brandData.slice(0, 10)
    }
  },
  mounted: function () {
    this.$nextTick(() => {      
      let brandName = document.querySelector('#brandName')
      this.Width = brandName.getBoundingClientRect().width
      this.Top = brandName.getBoundingClientRect().top
      this.Height = brandName.getBoundingClientRect().height
      this.Left = brandName.getBoundingClientRect().left
    })
    // 监听滚动方向
    /* let oldTop = 0; //旧数据，初始为0
    // 将自定义方法绑定到窗口滚动条事件
    window.onscroll = () => {
      let top = document.scrollingElement.scrollTop; //触发滚动条，记录数值
      //旧数据大于当前位置，表示滚动条top向上滚动
      if (oldTop >= top) {
        this.show = true
        oldTop = top
      } else {
        //相反...
        this.show = false
        oldTop = top        
      }
    } */
  },
  created() {
      let goodsname = getUrlParams().goodsname
      if (goodsname) {
        this.fullName = decodeURI(goodsname)
        this.readonly = true
      }
  },
  methods: {
    // 返回
    goback () {
      window.history.go(-1);
    },
    // 品牌输入框获取品牌列表数据
    getBrandName (val) {      
      Axios.post('/brand/brand/searchBrand.do', {
        brandName: val
      }).then(res => {
        if (res.respCode === "S") {
          this.brandData = res.data
        }        
      }).catch(err => {
        console.error(err)
      })
    },
    // 点击品牌列表
    getBrand (index) {
      this.isTap = true
      this.brandName = this.brandData[index].name
      this.brandData = []
    },
    // 搜索框获取焦点事件
    getFocus () {
      this.isTap = false
    },
    // 搜索按钮
    /* submit () {
      this.loading = true
      this.currentPage = 1
      this.resultList = null
      this.searchData()
    },
    loadMore () {// 滚动加载更多商品
      this.currentPage ++
      if(this.currentPage > this.totalPage || this.currentPage == 0){
        this.loading = true
        return false
      }
      this.searchData()
    }, */
    // 搜索商品数据
    /* searchData () {
      if (this.brandId!=='' || this.fullName!=='' || this.valueName!=='') {
        Axios.post('/goods/goods/customSearch.do', {
          brandName: this.brandName,
          fullName: this.fullName,
          valueName: this.valueName,
          currentPage: this.currentPage
        }).then(res => {
          if (res.respCode === "S") {
            let list = res.data.data            
            list.forEach(result => {
              result.minPrice = result.minPrice / 100
            })
            if (this.currentPage == 1) {
              this.resultList = list
            } else {
              this.resultList = this.resultList.concat(list)
            }
            this.totalPage = res.data.totalPage
            this.currentPage = res.data.currentPage

            if (this.currentPage < this.totalPage) {
              this.loading = false
            }
            this.$nextTick(() => {
              let h3 = document.querySelector('.shop-content').querySelectorAll('h3')
              let p = document.querySelector('.shop-content').querySelectorAll('p')
              if (this.currentPage == 1) {
                var i = 0
              } else {
                i = (this.currentPage - 1) * this.pageSize
              }
              for (let j = i; j < h3.length; j++) {
                if (h3[j].getBoundingClientRect().height > 21) {
                  p[j].style.bottom = "10vw"
                }
              }
            })
          } else {
            this.resultList = []
          }    			
        }).catch(err => {
          console.error(err)
        })
      }      
    }, */
    // 直接下单
    placeOrder () {
      if (this.brandName!==''&&this.fullName!==''&&this.valueName!=='') {
        Axios.post('/order/custom/insertMessage.do', {
          brandName: this.brandName,
          goodsName: this.fullName,
          attrValue: this.valueName
        }).then(res => {
          if (res.respCode === "S") {
            this.$createDialog({
              type: 'alert',
              title: '温馨提示',
              content: '你的搜索信息已提交给系统<br>稍后请到待查询订单查看！',
              confirmBtn: {
                text: '确定',
                active: true,
                disabled: false,
                href: 'searchList.html'
              }
            }).show()
          }          
        }).catch(err => {
          console.error(err)
        })
      } else {
        this.$createDialog({
          type: 'alert',
          title: '温馨提示',
          content: '请填写完整的信息！'
        }).show()
      }
    },
    // 其它超市按钮
    /* otherPrice (index) {
      this.name = this.resultList[index].fullName
      Axios.post('/goods/goods/supermarketPriceList.do', {
        goodsSkuId: this.resultList[index].goodsSkuId
      }).then(res => {
        if (res.respCode === "S") {
          let list = res.data.supermarketGoodsList
          list.forEach(goods => {
            if (goods.price == -1) {
              goods.price = "暂无价格"
            } else {
              goods.price = goods.price / 100 + "元"
            }
          });
          this.spreadRate = res.data.spreadRate        
          this.marketlist = list
          this.dialogShow = true
          this.otherMarket = true
        }
        
			}).catch(err => {
        console.error(err)
      })
    }, */
    // 加入购物车按钮
    /* cartdialogShow (index) {
      this.dialogShow = true
      this.cartShow = true
      this.curIndex = index
    },
    // 取消按钮
    btnCancel () {
      this.dialogShow = false
      this.otherMarket = false
      this.cartShow = false
      this.tipshow = false
    },
    // 加号按钮
    btnPlus () {
      if (this.num < 99) {
        this.num = this.num + 1
      }
    },
    btnReduce () { // 减号按钮
      if (this.num > 1) {
        this.num = this.num - 1
      }
    },
    // 加入购物车确定按钮
    btnOk () {
      Axios.post('/goods/shopCar/addShopCar.do', {
        goodsSkuId: this.resultList[this.curIndex].goodsSkuId,
        quantity: this.num
      }).then(res => {        
        this.errMsg = "您已提交成功<br/>请到购物车查看"
      }).catch(err => {
        console.error(err)
      })
      this.dialogShow = false
      this.cartShow = false
      this.num = 1
    } */
  }
})